package com.codemart.lanze.core.freemarker;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codemart.lanze.modules.cms.entity.FriendLink;
import com.codemart.lanze.modules.cms.service.IFriendLinkService;

import freemarker.core.Environment;
import freemarker.ext.beans.ArrayModel;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 友情链接指令
 *
 * @author lanze
 */
@SuppressWarnings({ "rawtypes", "deprecation" })
@Repository
public class DirectiveFriendLinkList implements TemplateDirectiveModel {

	@Autowired
	private IFriendLinkService friendLinkService;

	@Override
	public void execute(Environment env, Map map, TemplateModel[] loopVars, TemplateDirectiveBody body)
			throws TemplateException, IOException {

		List<FriendLink> list = friendLinkService.list(new QueryWrapper<>());
		loopVars[0] = new ArrayModel(list.toArray(), new BeansWrapper());
		body.render(env.getOut());

	}

}
